//
//  PersonalHomePageAllController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/24.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PersonalHomePageAllController.h"
#import "PersonalHomePageCell.h"
#import "PersonalHomePageModel.h"
#import "PersonalHomePageArticleCell.h"
#import "PersonalHomePageVideoCell.h"
#import "PersonalHomePageSmallVideoCell.h"
#import "PersonalHomePageQuestionAnswerCell.h"
#import "PersonalHomePageFoodCell.h"


@interface PersonalHomePageAllController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
@property (nonatomic , strong)NSMutableArray *dataArray;

@end

@implementation PersonalHomePageAllController

- (void)loadView{
    [super loadView];
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    //把滚动视图交给管理器
    if (self.sManager) {
        [self.sManager addChildView:self.listView];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalHomePageCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageCell class]) forIndexPath:indexPath];
    PersonalHomePageArticleCell * cell1 = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageArticleCell class]) forIndexPath:indexPath];
    PersonalHomePageVideoCell * cell2 = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageVideoCell class]) forIndexPath:indexPath];
    PersonalHomePageQuestionAnswerCell * cell4 = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageQuestionAnswerCell class]) forIndexPath:indexPath];

    PersonalHomePageModel * model = self.dataArray[indexPath.section];
    
    if (model.type == PersonalHomePageArticle) {
        cell1.model = model;
        return cell1;
    }
    if (model.type == PersonalHomePageVideo) {
        cell2.model = model;
        return cell2;

    }
    if (model.type == PersonalHomePageQanswer) {
        cell4.model = model;
        return cell4;

    }
    cell.model = model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController pushViewController:[ImageAndTextDetailController new] animated:YES];
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

//        _listView.tableFooterView = UIView.new;
        [_listView registerClass:[PersonalHomePageCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageCell class])];
        [_listView registerClass:[PersonalHomePageArticleCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageArticleCell class])];
        [_listView registerClass:[PersonalHomePageVideoCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageVideoCell class])];
        [_listView registerClass:[PersonalHomePageQuestionAnswerCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageQuestionAnswerCell class])];
        [_listView registerClass:[PersonalHomePageFoodCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageFoodCell class])];

    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PersonalHomePage" ofType:@"plist"];
                  //当数据结构为数组时
               NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
               [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                   PersonalHomePageModel *model = [PersonalHomePageModel new];
                   [model setValuesForKeysWithDictionary:obj];
                   [_dataArray addObject:model];
               }];
    }
    return _dataArray;
}
@end
